/**
 * @author  Administrator
 * @date    2016年1月20日
 */
package com.langxin.erp.util.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.util.CookieGenerator;

/**
 * cookie相关
 * @author guquanjiang
 * @date   2016年1月20日
 */
public class CookieUtil {

	/**
	 * 数据保存至cookie
	 * @date   2016年1月20日
	 * @param key
	 * @param maxAge
	 * @param path
	 * @param cookieValue
	 * @param response
	 */
	public static void saveToCookie(String key, Integer maxAge, String path, String cookieValue, HttpServletResponse response){
		CookieGenerator cookie = new CookieGenerator();
        cookie.setCookieName(key);
        cookie.setCookieMaxAge(maxAge);
        cookie.setCookiePath(path);
        cookie.addCookie(response, cookieValue);
	}
	
	
	/**
     * 获取cookie
     * @param request
     * @param cookieName
     * @return
     */
    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if(cookies!=null && cookies.length > 0) {
            for(Cookie c : cookies) {
                if(c.getName().equals(cookieName)) {
                    return c.getValue();
                }
            }
        }
        return null;
    }
  
    /**
     * 创建Cookie
     * @param response HTTP Response
     * @param cookieName Cookie名称
     * @param cookieValue Cookie值
     * @param cookieDomain Cookie域
     * @param cookiePath Cookie路径
     * @param cookieMaxAge Cookie有效时间
     */
    public static void createCookie(HttpServletResponse response, String cookieName,
                                    String cookieValue, String cookieDomain, String cookiePath,
                                    Integer cookieMaxAge) {
        CookieGenerator cookie = new CookieGenerator();
        cookie.setCookieName(cookieName);
        cookie.setCookieDomain(cookieDomain);
        cookie.setCookiePath(cookiePath);
        cookie.setCookieMaxAge(cookieMaxAge);
        cookie.addCookie(response, cookieValue);
    }
    
    /**
     * 创建Cookie
     * @param response
     * @param cookieName
     * @param cookieValue
     * @param cookieDomain
     * @param cookiePath
     */
    public static void createCookie(HttpServletResponse response, String cookieName,
                                    String cookieValue, String cookieDomain, String cookiePath) {
        CookieGenerator cookie = new CookieGenerator();
        cookie.setCookieName(cookieName);
        cookie.setCookieDomain(cookieDomain);
        cookie.setCookiePath(cookiePath);
        cookie.setCookieMaxAge(Integer.MAX_VALUE);
        cookie.addCookie(response, cookieValue);
    }
    
    /**
     * 创建Cookie
     * @param response
     * @param cookieName
     * @param cookieValue
     * @param cookieDomain
     */
    public static void createCookie(HttpServletResponse response, String cookieName,
                                    String cookieValue, String cookieDomain) {
        CookieGenerator cookie = new CookieGenerator();
        cookie.setCookieName(cookieName);
        cookie.setCookieDomain(cookieDomain);
        cookie.setCookiePath("/");
        cookie.setCookieMaxAge(Integer.MAX_VALUE);
        cookie.addCookie(response, cookieValue);
    }
    
    /**
     * 删除Cookie
     * @param response
     * @param cookieName
     */
    public static void removeCookie(HttpServletResponse response, String cookieName) {
        CookieGenerator cookie = new CookieGenerator();
        cookie.setCookieName(cookieName);
        cookie.removeCookie(response);
    }
    
   
}
